package com.myproject.jdbc;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Jdbc1 {

    public static void main(String[] args) throws Exception{

        Connection connection = null;
        connection = DriverManager.getConnection("jdbc:mysql://118.89.68.96:3063/crm?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false","root","root123");
        Statement statement = connection.createStatement();

        List<String> idList = new ArrayList<>();

        {
            String sql = "select id from t_blog";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()){
                String id = rs.getString("id");
                idList.add(id);
            }
        }

        for (String id:idList) {

            String tag_id = null;
            {
                String sql = "select tag_id from t_blog where id = "+id;
                ResultSet rs = statement.executeQuery(sql);
                if (rs.next()){
                    tag_id = rs.getString("tag_id");
                }
            }
            System.out.println(tag_id);

            String name = null;
            {
                String sql = "select name from t_blog_tag where id = "+tag_id;
                ResultSet rs = statement.executeQuery(sql);
                if (rs.next()){
                    name = rs.getString("name");
                }
            }
            System.out.println(name);

            {
                String sql = "update t_blog set tag='"+name+"' where id = "+id;
                statement.executeUpdate(sql);
            }

        }

    }
}
